﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PublicOfficialSelectionService;
using CommunitySystem.Views;
using CommunitySystem.ZhaoLuService;
using EHR.Common;

namespace CommunitySystem
{
    public partial class ZhaoLuKaoGuanControl : UserControl
    {
        private PublicOfficialSelectionServiceClient Client = new PublicOfficialSelectionServiceClient();
        private ZhaoLuService.ZhaoLuServiceClient Client1 = new ZhaoLuServiceClient();
        private ObservableCollection<PublicOfficialSelectionService.KaoGuanModel> selectedempmodel =
            new ObservableCollection<PublicOfficialSelectionService.KaoGuanModel>();
        private ZhaoLuService.ZhaoLuJiHuaModel poi = new ZhaoLuService.ZhaoLuJiHuaModel();
        private readonly MouseClickManager _gridClickManagerXQ;
        public ZhaoLuKaoGuanControl()
        {
            InitializeComponent();
            _gridClickManagerXQ = new MouseClickManager(DGRole, 300);
            _gridClickManagerXQ.DoubleClick += new System.Windows.Input.MouseButtonEventHandler(_gridClickManagerXQ_DoubleClick);
            Load();
        }
        public void Load()
        {
            Client.GetKaoGuanModelCompleted += new EventHandler<GetKaoGuanModelCompletedEventArgs>(Client_GetKaoGuanModelCompleted);
            Client.GetKaoGuanModelAsync(name.Text, 0, Session.LoginUser.LoginName, "招录工作");
            Client1.GetZhaoLuJiHuaModelCompleted += new EventHandler<GetZhaoLuJiHuaModelCompletedEventArgs>(Client1_GetZhaoLuJiHuaModelCompleted);
            Client1.GetZhaoLuJiHuaModelAsync(0, "0",0);
        }
        private void dgXQ_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.MouseLeftButtonUp += _gridClickManagerXQ.HandleClick;
        }

        private void _gridClickManagerXQ_DoubleClick(object sender, MouseButtonEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as KaoGuanModel;
            ZhaoLuKaoGuanWindow RMWindow = new ZhaoLuKaoGuanWindow(Convert.ToInt32(seljw.ID), false);
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }

        void Client1_GetZhaoLuJiHuaModelCompleted(object sender, GetZhaoLuJiHuaModelCompletedEventArgs e)
        {
            if (e.Result != null&&e.Result.Count>0)
                poi = e.Result[0];
        }

        void Client_GetKaoGuanModelCompleted(object sender, GetKaoGuanModelCompletedEventArgs e)
        {
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            ZhaoLuKaoGuanWindow RMWindow = new ZhaoLuKaoGuanWindow(0, true);
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            Load();
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as KaoGuanModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                ZhaoLuKaoGuanWindow RMWindow = new ZhaoLuKaoGuanWindow(Convert.ToInt32(seljw.ID), false);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的考官信息！");
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            if (DGRole.ItemsSource != null)
            {
                foreach (KaoGuanModel i in (ObservableCollection<KaoGuanModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string ids = "";

                    foreach (KaoGuanModel i in selectedempmodel)
                    {
                        ids += i.ID.ToString() + ",";
                    }
                    Client.DeteleKaoGuanModelCompleted -= new EventHandler<DeteleKaoGuanModelCompletedEventArgs>(Client_DeteleKaoGuanModelCompleted);
                    Client.DeteleKaoGuanModelCompleted += new EventHandler<DeteleKaoGuanModelCompletedEventArgs>(Client_DeteleKaoGuanModelCompleted);
                    Client.DeteleKaoGuanModelAsync("(" + ids.Substring(0, ids.Length - 1) + ")");
                }
            }
        }

        void Client_DeteleKaoGuanModelCompleted(object sender, DeteleKaoGuanModelCompletedEventArgs e)
        {
            Load();
        }

        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (KaoGuanModel p in DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                //CheckBox cb1 = DGRole.Columns[0].GetCellContent(p) as CheckBox;
                //cb1.IsChecked = p.IsSelected;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }

        }

        private int cou = 0;
       

        /// <summary>
        /// 招录工作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZL_Click(object sender, RoutedEventArgs e)
        {
            cou = 0;
            if (DGRole.ItemsSource != null)
            {
                foreach (KaoGuanModel i in (ObservableCollection<KaoGuanModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                string cjzlname = "";
                string kgid = "";
                foreach (KaoGuanModel i in selectedempmodel)
                {
                    KaoGuanDetailModel model1 = new KaoGuanDetailModel();
                    model1.KaoGuanID = i.ID;
                    model1.Year = poi.ID;
                    model1.XBType = "招录工作";
                    //参加了招录工作的不能进行再次操作
                    if (i.Iszl.Equals("是"))
                    {
                        cjzlname += i.Name + ",";
                    }
                    kgid += i.ID.ToString()+",";
                    //Client.AddKaoGuanDetailModelCompleted -= new EventHandler<AddKaoGuanDetailModelCompletedEventArgs>(Client_AddKaoGuanDetailModelCompleted);
                    //Client.AddKaoGuanDetailModelCompleted += new EventHandler<AddKaoGuanDetailModelCompletedEventArgs>(Client_AddKaoGuanDetailModelCompleted);
                    //Client.AddKaoGuanDetailModelAsync(model1);
                    //cou += 1;
                }
                if (!string.IsNullOrEmpty(cjzlname))
                    MessageBox.Show(cjzlname + " 考官已经参加该招录工作,请选择没有参加该招录工作的人员进行参加！","系统提示",MessageBoxButton.OK);
                else
                {
                    Client.AddKaoGuanDetailModelBykgidsCompleted -= new EventHandler<AddKaoGuanDetailModelBykgidsCompletedEventArgs>(Client_AddKaoGuanDetailModelBykgidsCompleted);
                    Client.AddKaoGuanDetailModelBykgidsCompleted += new EventHandler<AddKaoGuanDetailModelBykgidsCompletedEventArgs>(Client_AddKaoGuanDetailModelBykgidsCompleted);

                    Client.AddKaoGuanDetailModelBykgidsAsync(kgid, "招录工作", poi.ID);
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择信息！");
        }

        void Client_AddKaoGuanDetailModelBykgidsCompleted(object sender, AddKaoGuanDetailModelBykgidsCompletedEventArgs e)
        {
           if(e.Result)
               SilverlightMessageBoxLibrary.Message.InfoMessage("添加成功！");
        }

        void Client_AddKaoGuanDetailModelCompleted(object sender, AddKaoGuanDetailModelCompletedEventArgs e)
        {
            if (e.Result == true && cou == selectedempmodel.Count)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("添加成功！");
                cou = 0;
            }
            if (e.Result == false && cou == selectedempmodel.Count)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("该考官已经参加该招录工作！");
                cou = 0;
            }
        }

    }
}
